<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
		<button type="primary" class="btn-login" open-type="getUserInfo" @click="getUserInfo2">一键登录</button>
		<!-- @click="getUserInfo2" -->
		<!-- open-type="getUserInfo" @getuserinfo="getUserInfo" -->
		<text class="tips-text">登录后尽享更多权益</text>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-login",
		computed: {
			...mapState('m_user', ['redirectInfo'])
		},
		data() {
			return {
				// 用户信息
				// userInfo: {}
			};
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
			/* 
			获取用户的基本信息 
			 */
			// getUserInfo(e) {
			// 	console.log(e, '---------------');
			// },
			/* 
			 最新获取用户的基本信息方式
			 */
			async getUserInfo2() {
				const [err, succ] = await uni.getUserProfile({
					desc: '获取用户信息'
				}).catch((err) => err)
				if (err === null && succ) {
					this.updateUserInfo(succ.userInfo)
				}
				this.getToken(succ)
			},
			/* 
			 获取token
			 */
			async getToken(info) {
				// 使用login获取code
				const [err, succ] = await uni.login().catch(err => err)
				if (err || succ.errMsg !== 'login:ok') return uni.$showMsg('登录失败!')
				const data = {
					code: succ.code || '',
					encryptedData: info.encryptedData || '',
					iv: info.iv || '',
					rawData: info.rawData || '',
					signature: info.signature || ''
				}
				console.log(data.code, '最新的code');
				const res = await uni.$http.post('/api/public/v1/users/wxlogin', data)
				console.log(res, '获取token');
				// if (res.data.meta.status !== 200) return uni.$showMsg('登录失败!')
				// 暂时登录接口不好使,假装cookies是token,进行下一步操作
				this.updateToken(res.cookies[0])
				// 获取成功token之后,跳转到重定向的位置
				if (this.redirectInfo) {
					if (this.redirectInfo.openType === 'switchTab') {
						uni.switchTab({
							url: this.redirectInfo.form,
							complete: () => {
								this.updateRedirectInfo(null)
							}
						})
					} else {
						uni.navigateTo({
							url: this.redirectInfo.form,
							complete: () => {
								this.updateRedirectInfo(null)
							}
						})
					}
				}
				if (res.data.meta.status !== 200) return uni.$showMsg('登录失败!')
				// uni.$showMsg('登录成功!')
				// this.updateToken(res.data.message.token)
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		height: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #f8f8f8;
		position: relative;
		overflow: hidden;

		&::after {
			content: ' ';
			display: block;
			height: 40px;
			width: 100%;
			background-color: white;
			border-radius: 100%;
			position: absolute;
			bottom: 0;
			left: 0;
			transform: translateY(50%);
		}

		.btn-login {
			width: 90%;
			margin: 15px 0;
			border-radius: 50px;
			background-color: #c00000;
		}

		.tips-text {
			color: gray;
			font-size: 12px;
			margin-top: 10px;
		}
	}
</style>